package com.xinqi.modules.prepare.video.manager.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xinqi.common.base.error.CommonErrors;
import com.xinqi.common.mybatis.data.service.impl.BaseServiceImpl;
import com.xinqi.modules.prepare.video.convert.PrepareVideoPointConvert;
import com.xinqi.modules.prepare.video.domain.PrepareVideoEntity;
import com.xinqi.modules.prepare.video.domain.PrepareVideoPointEntity;
import com.xinqi.modules.prepare.video.dto.req.PrepareVideoPointQueryDTO;
import com.xinqi.modules.prepare.video.dto.rsp.PrepareVideoPointResultDTO;
import com.xinqi.modules.prepare.video.manager.PrepareVideoPointManager;
import com.xinqi.modules.prepare.video.mapper.PrepareVideoPointMapper;
import lombok.RequiredArgsConstructor;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 备课区互动视频环节管理层实现
 *
 * @author: system
 * @date: 2023/03/19
 */
@Service
@RequiredArgsConstructor
public class PrepareVideoPointManagerImpl extends BaseServiceImpl<PrepareVideoPointMapper, PrepareVideoPointEntity> implements PrepareVideoPointManager {
    private final PrepareVideoPointConvert prepareVideoPointConvert;

    /**
     * 自定义分页查询
     */
    @Override
    public Page<PrepareVideoPointResultDTO> findPage(Page<Object> page, PrepareVideoPointQueryDTO query) {
        return this.baseMapper.findPage(page, query);
    }

    /**
     * 自定义列表查询
     */
    @Override
    public List<PrepareVideoPointResultDTO> findList(PrepareVideoPointQueryDTO query) {
        return this.baseMapper.findList(query);
    }

    /**
     * 根据id查询 备课区互动视频环节数据
     */
    @Override
    public PrepareVideoPointEntity findById(Long id) {
        CommonErrors.BAD_REQUEST.check(Objects.nonNull(id), "ID不能为空");
        return getById(id);
    }

    /**
     * 根据id删除 备课区互动视频环节数据
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteById(Long id) {
        CommonErrors.BAD_REQUEST.check(Objects.nonNull(id), "ID不能为空");
        return this.removeById(id);
    }

    @Override
    public List<PrepareVideoPointEntity> findBVideoId(Long videoId) {
        LambdaQueryWrapper<PrepareVideoPointEntity> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(PrepareVideoPointEntity::getVideoId, videoId);
        return list(wrapper);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void copy(PrepareVideoEntity source, PrepareVideoEntity target) {
        List<PrepareVideoPointEntity> list = findBVideoId(source.getId());
        list.forEach(v -> copy(v, target));
    }

    private void copy(PrepareVideoPointEntity source, PrepareVideoEntity target) {
        PrepareVideoPointEntity entity = prepareVideoPointConvert.copy(source, new PrepareVideoPointEntity());
        entity.setPrepareId(target.getPrepareId());
        entity.setVideoId(target.getId());
        save(entity);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void move(PrepareVideoEntity entity) {
        LambdaUpdateWrapper<PrepareVideoPointEntity> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(PrepareVideoPointEntity::getVideoId, entity.getId());
        wrapper.set(PrepareVideoPointEntity::getPrepareId, entity.getPrepareId());
        this.update(wrapper);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean deleteByVideoId(Long videoId) {
        List<PrepareVideoPointEntity> partList = findBVideoId(videoId);
        if (CollectionUtils.isNotEmpty(partList)) {
            deletedByIds(partList.stream().map(PrepareVideoPointEntity::getId).collect(Collectors.toList()));
        }
        return true;
    }

}
